package com.mmall.service.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import com.mchange.v1.util.CollectionUtils;
import com.mmall.common.ServerResponse;
import com.mmall.common.TokenCache;
import com.mmall.dao.CategoryMapper;
import com.mmall.pojo.Category;
import com.mmall.service.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Set;

@Service("categoryService")
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    private static Logger logger = LoggerFactory.getLogger(TokenCache.class);

    public ServerResponse addCategory(String categoryname, Integer parentId){

        if(parentId==null|| StringUtils.isBlank(categoryname)){
            return ServerResponse.createServerResponseByFailure("参数类型错误");
        }
        Category category = new Category();
        category.setName(categoryname);
        category.setParentId(parentId);
        category.setStatus(true);//该类是可用的
        int result = categoryMapper.insert(category);
        if(result==1){
            return ServerResponse.createServerResponseBySuccess("分类创建成功");
        }
        return ServerResponse.createServerResponseByFailure("分类创建失败");


    }

    public ServerResponse<List<Category>> getChildrenCategory(Integer parentId){
        List<Category> categoryList=categoryMapper.selectCategoryByParentId(parentId);
        if(categoryList.size()==0){
            logger.info("未找到当前分类的子分类");
        }
        return ServerResponse.createServerResponseBySuccess(categoryList);
    }


    public ServerResponse getChildrenAllCategory(Integer categoryId){

        Set<Category> categorySet = Sets.newHashSet();
        getChildrenAllCategoryDigui(categorySet,categoryId);
        List<Integer> categoryIdList = Lists.newArrayList();
        if(categoryId!=null){
            for(Category category:categorySet){
                categoryIdList.add(category.getId());
            }
        }
        return  ServerResponse.createServerResponseBySuccess(categoryIdList);
    }


    public Set<Category> getChildrenAllCategoryDigui(Set<Category> categorySet,Integer categoryId){
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        if (category!=null){
            categorySet.add(category);
        }

        List<Category>   categoryList=categoryMapper.selectCategoryByParentId(categoryId);
        if (categoryList!=null){
            for (Category category2:categoryList){
                getChildrenAllCategoryDigui(categorySet,category2.getId());
            }
        }
        return categorySet;


    }


    public ServerResponse updateCategoryname(Integer categoryId,String categoryName){

        if(categoryId==null|| StringUtils.isBlank(categoryName)){
            return ServerResponse.createServerResponseByFailure("参数类型错误");
        }
        int result = categoryMapper.updateCategoryNameByPrimaryKey(categoryId,categoryName);
        System.out.printf("更新操作result"+result);
        if(result==1){
            return ServerResponse.createServerResponseBySuccess("更新成功");

        }
        return ServerResponse.createServerResponseByFailure("更新失败");

    }









    }
